<?php
require 'config/config.php';
// ----------------------------------------------
//  Instantiate MAIN CORE class
// ----------------------------------------------    
require 'core/main.class.php';
$MAIN = new main(); 
require_once("core/sessions.class.php");
$SESS = new Session();
require_once("core/promotion.class.php");
require_once("core/promotionalItem.class.php");	
require_once("display/adminposdisplay.class.php");
require_once("functions/stringFunctions.php");

$SF = new StringFunctions();

// get promotion data from previous page

$promotion = $SESS->getSessionObject('Promotion');
$promotion = $SESS->unserializeObject($promotion);


	

// begin bulding promotion ready for entry into database;


$extrasList = array();

if ( isset($_POST['writeNumberOfItemsVal1']) ) {
	$extrasList['writeNumberOfItems'] = array();
	$extrasList['writeNumberOfItems'][] = $_POST['writeNumberOfItemsVal1'];
	}
if ( isset($_POST['foodItemsList']) ) {
	$extrasList['foodItemsList'] = array();
	$extrasList['foodItemsList'][] = $_POST['foodItemsList'];
	}
if ( isset($_POST['writeDays']) ) {
	$days = "";
	foreach ( $_POST['writeDays'] as $key => $val){
		$days .= $val.",";
	}
	// REMOVE LAST COMMA
	$days = $SF->removeLastCharacter($days);
	$extrasList['writeDays'][] = $days;
	}
if ( isset($_POST['writeOwnWords']) ) {
	$extrasList['writeOwnWords'][] = TRUE;
	}
if ( isset($_POST['addOwnPrice']) ) {
	$extrasList['addOwnPrice'][] = TRUE;
	}
if ( isset($_POST['pricePoint']) ) {
	$extrasList['pricePoint'] = array();
	array_push($extrasList['pricePoint'], $SF->formatPrice($_POST['pricePointVal1']), $SF->formatPrice($_POST['pricePointVal2']), $SF->formatPrice($_POST['pricePointVal3']));
	}
if ( isset($_POST['writeTimes']) ) {
	$extrasList['writeTimes'] = array();
	array_push($extrasList['writeTimes'], $_POST['writeTimesVal1'], $_POST['writeTimesVal2'], $_POST['writeTimesVal3'], $_POST['writeTimesVal4'], $_POST['writeTimesVal5']);
	}
if ( isset($_POST['addLogo']) ) {
	$extrasList['addLogo'][] = TRUE;
	}
if ( isset($_POST['addOffer']) ) {
	$extrasList['addOffer'][] = TRUE;
	}
	
// SERIALIZE TO EXTRAS ARRAY
$extrasArray = $SESS->saveObject($extrasList);

// get document root and add the promoimages folder
if (ISSET($_FILES['images'])){
$server = $_SERVER['SCRIPT_PATH'];
$imageNameArray = array();
while(list($key,$value) = each($_FILES['images']['name'])) {
	array_push($imageNameArray, $_FILES['images']['name'][$key]);
	if(!empty($value))
			{
				$filename = $value;
					$filename=str_replace(" ","_",$filename);// Add _ inplace of blank space in file name, you can remove this line

					$add = realpath(SITEBASE."/".PROMOIMAGES."/".$filename);
                    
					copy($_FILES['images']['tmp_name'][$key], $add);
					chmod("$add",0777);
			}
	}

	// CREATE SQL FOR INSERT IMAGES INTO DATABASE
	$imageSql = "";
	foreach ($imageNameArray as $key => $val){
		$imageSql .= str_replace(" ","_",$val).";";
	}
}else{
	$imageSql = ""; // THERE ARE NO IMAGES
}
// remove trailing ";"
$imageSql = $SF->removeLastCharacter($imageSql);


// SERIALIZE THE PROMOTIONAL ITEMS FROM THE PROMOTION OBJECT TO STORE IN THE DATABASE

$promotionalItems = $SESS->saveObject($promotion->getPromotionalItems());


// GRAB THE DISCOUNT FILE IF ANY AND FORMAT IT FOR THE DATABASE


	$value = $_FILES['discountTable']['name'];
	if(!empty($value))
			{
				$filename = $value;
					$filename=str_replace(" ","_",$filename);// Add _ inplace of blank space in file name, you can remove this line

					$add = realpath($_SERVER['DOCUMENT_ROOT'])."/downloads/".$filename;
                    
					copy($_FILES['discountTable']['tmp_name'], $add);
					chmod("$add",0777);
			}


if ($promotion != NULL){

$promotionName = $promotion->getPromotionName();
$promotionSector = $promotion->getSector();
$promotionCategory = $promotion->getCategory();

require_once("db/dbPromo.class.php");
$DB = new DbPromo();


if ($_POST['subCategory']) {
	$sql = "INSERT INTO `subcategories` (`id` , `sectionId` , `title` ) VALUES ( NULL , '".$promotion->getSector()."', '".$_POST['subCategory']."');";
	$result = $DB->addSubCategory($sql);
	
	$sql = "select last_insert_id()";
	$result = $DB->selectQuery($sql);
	
	$id = mysql_fetch_array($result);
	//print_r($id);
	$lastid = $id[0];
	
	$promotion->setSubCategory($lastid);
}else{
	$promotion->setSubCategory($_POST['subCategoryId']);
}
	
	
// build sql 
if($_POST['promoId'] == ""){
	$sql = "INSERT INTO `promotions` VALUES ('','".$promotionSector."','".$promotionCategory."','".$promotionName."','".$imageSql."','".$promotionalItems."','deliveryCost','".$extrasArray."', '".$promotion->getSubCategory()."', '".$promotion->startDate."', '".$promotion->endDate."', '".$promotion->targetDate."', '".$promotion->frontPage."')";
	$DB->addPromotion($sql, $promotionSector);
}else{
	$sql = "UPDATE `promotions` SET promotionalItems = '".$promotionalItems."', extras = '".$extrasArray."' WHERE id = '".$_POST['promoId']."'";
	$DB->addPromotion($sql, $promotionSector);
}


}


	
	


?>